package com.example.thirdstage_project.controller;

/**
 * @author 86158
 * @version 1.0
 * Create by 2022/11/26 10:40
 */


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.thirdstage_project.common.Code;
import com.example.thirdstage_project.common.Result;
import com.example.thirdstage_project.entity.Unit;
import com.example.thirdstage_project.entity.User;
import com.example.thirdstage_project.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单位信息管理
 */

@RestController
@RequestMapping("/units")
@Slf4j
public class UnitController {


    @Autowired
    private UnitService unitService;
    /**
     * 分页
     * @return
     */
    @GetMapping("/all")
    public Result getPage2(int currentPage, int pageSize){
        IPage<User> page = unitService.getPage(currentPage, pageSize);
        if (currentPage > page.getPages()){
            page = unitService.getPage((int)page.getPages(), pageSize);
        }
        return new Result(Code.Page_OK,page);
    }
    /**
     * 查询
     */


    @GetMapping
    public Result list(){
        List<Unit> list = unitService.select();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败";
        return new Result(code,list,msg);
    }


    /**
     * 删除
     */
    @DeleteMapping("/{unitid}")
    public Result delete(@PathVariable Integer id){
        boolean delete = unitService.removeById(id);
        return new Result(delete ? Code.DELETE_OK: Code.DELETE_ERR ,delete);
    }



    /**
     * 更新
     */
//    @PostMapping()
//    public Result update(Unit unit){
//        boolean update = unitService.updateUnit(unit);
//        return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERR,update);
//    }



    /**
     * 根据密码查uid
     */
    @GetMapping("/selectUid")
    public Result listuid(String password){
        Integer id = unitService.selectuid(password);
        Integer code = id != null ? Code.GET_OK : Code.GET_ERR;
        String msg = id != null ? "" : "数据查询失败";
        return new Result(code,id,msg);
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(String password ,int uid ){
        boolean id = unitService.updatePassword(password ,uid);
        return new Result(id ? Code.UPDATE_OK : Code.UPDATE_ERR,id);
    }

    /**
     * 模糊查询
     */
    @GetMapping("/search")
    public Result search(String name){
        String searchName = unitService.search(name);
        Integer code = searchName != null ? Code.GET_OK : Code.GET_ERR;
        String msg = searchName != null ? "" : "数据查询失败";
        return new Result(code,searchName,msg);
    }
}
